package fudan.se.project.repository.projection;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import fudan.se.project.domain.conference.Topic;
import fudan.se.project.domain.conference.post.Rebuttal;
import fudan.se.project.domain.conference.production.Paper;
import fudan.se.project.repository.projection.summary.RoleSummary;
import fudan.se.project.repository.projection.summary.TopicSummary;
import fudan.se.project.repository.projection.summary.WriterSummary;

import java.util.*;

public class PaperProjection {


    private final Rebuttal rebuttal;
    private final Paper paper;


    private PaperProjection(Paper paper) {
        this.rebuttal = null;
        this.paper = paper;
    }

    private PaperProjection(Rebuttal rebuttal) {
        this.rebuttal = rebuttal;
        this.paper = rebuttal.getPost().getPaper();
    }


    public String getRebuttal() {
        if (null == this.rebuttal) {
            return null;
        }
        return this.rebuttal.getArgument();
    }

    public String getTitle() {
        return this.paper.getTitleText();
    }

    public String getAbstract() {
        return this.paper.getAbstractText();
    }

    public ConferenceProjection getConference() {
        return ConferenceProjection.project(this.paper.getConference());
    }

    @JsonIgnoreProperties("paper")
    public List<ReviewInfoProjection> getReviewInfos() {
        return ReviewInfoProjection.project(this.paper.getReviewInfos());
    }


    public String getOriginalFilename() {
        return this.paper.getOriginalFilename();
    }


    public int getPaperState() {
        return this.paper.getPaperState().ordinal();
    }

    public Date getDateSubmitted() {
        return this.paper.getDateSubmitted();
    }

    public Long getId() {
        return this.paper.getId();
    }

    public String getDownloadUrl() {
        return "/conference/paper/download?id=" + getId();
    }

    public RoleSummary getAuthor() {
        return new RoleSummary(this.paper.getAuthor());
    }

    public Set<TopicSummary> getTopics() {
        HashSet<TopicSummary> summaries = new HashSet<>();

        for (Topic topic : this.paper.getTopics()) {
            summaries.add(new TopicSummary(topic));
        }

        return summaries;
    }

    public List<WriterSummary> getWriters() {
        return WriterSummary.summarize(this.paper.getWriters());
    }

    public static PaperProjection project(Paper paper) {
        return new PaperProjection(paper);
    }

    public static PaperProjection project(Rebuttal rebuttal) {
        return new PaperProjection(rebuttal);
    }

    public static List<PaperProjection> project(Collection<Paper> values) {
        ArrayList<PaperProjection> arrayList = new ArrayList<>(values.size());
        for (Paper value : values) {
            arrayList.add(project(value));
        }
        return arrayList;
    }

    public static List<PaperProjection> projectRebuttals(Collection<Rebuttal> values) {
        ArrayList<PaperProjection> arrayList = new ArrayList<>(values.size());
        for (Rebuttal value : values) {
            arrayList.add(project(value));
        }
        return arrayList;
    }
}
